package controller;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@CrossOrigin(origins = "http://localhost:5500",allowCredentials = "true")
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    //生成动态字符串验证码
    @RequestMapping("/createSpec.do")
    public void createSpec(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置图片宽高
            SpecCaptcha captcha = new SpecCaptcha(130, 38);
            //设置验证码长度
            captcha.setLen(4);
            CaptchaUtil.out(captcha, request, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //检查验证码
    @RequestMapping("verify.do")
    public Object verify(String verifyCode,HttpServletRequest request){
        //System.out.println("verifyCode:"+verifyCode);
        //String yzm  = (String) request.getSession().getAttribute("captcha");
        //System.out.println("yzm:"+yzm);
        //验证通过清理验证码
        if(CaptchaUtil.ver(verifyCode,request)){
            //CaptchaUtil.clear(request);
            return new Result<>("0","success","success");
        }else {
            return new Result<>("0","error","error");
        }
    }

}
